const app = getApp();
Page({  
  id:null,
  data: {  
     trainId:0,
     courseId:0,
     sapId:'',
     baseUrl: app.globalData.baseUrl,
     questionIndex:0,
     question:[],
     examInfo:null,
     buttons: [{ text: '再想想' }, { text: '确定' }],
     confirmAgainShow:false,//二次确认
     questionTypeName:'', //1:'选择题',//   2:'单选题',//   3:'多选题'
     questionTypeSum:0, // 当前问题类型总数
      questionTypeNo:0,  //当前问题类型当前序号
      questionTotal:0,
      questionNo:0
  },  
  onLoad:function(options){

     this.setData({
      trainId:options.trainId,
      courseId:options.courseId,
      sapId:options.sapId,
      projectType:options.projectType
     });
     this.loadData();
  },

  
  loadData:function(){
    var that=this;
    wx.request({
      url: that.data.baseUrl+'/v1/openApi/staff/myQuestion/'+that.data.sapId+'/' +that.data.trainId+'/'+that.data.courseId,
      method: 'GET',
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        if(res.data.code!=200){
          wx.showToast({  
            title: res.data.msg,  
            icon: 'none',  
            duration: 3000  
          });  
          return;
        }
        var data=res.data.data;
        if(data.value.length==0){
          wx.showToast({  
            title: '当前无考试',  
            icon: 'none',  
            duration: 3000  
          });  
          setTimeout(()=>{
            wx.navigateBack({
              delta: 1
            })
          },1000)
         
          return;
        }

        //排序先单选题 再多选题 再判断题
        const danXuanList = data.value.filter(item=>item.type==2)
        const duoXuanList = data.value.filter(item=>item.type==3)
        const panDuanList = data.value.filter(item=>item.type==1)
        that.setData({
          question: [...danXuanList,...duoXuanList,...panDuanList],
          examInfo:data.key
        })
        that.computedQuestionName();
      },fail(res){
          console.log(res);
          wx.showToast({  
            title: '考题查询失败，请联系管理人员！',  
            icon: 'none',  
            duration: 3000  
          });  
      }
    });
  },

  computedQuestionName(){
    let questionConst ={
        1:'判断题',
        2:'单选题',
        3:'多选题'
      }[this.data.question[this.data.questionIndex].type]
     
      //计算当前第几题
      const currentQustion = this.data.question[this.data.questionIndex];
      let tempQuestionList = this.data.question.filter(item=> item.type === currentQustion.type)
      const findIndex = tempQuestionList.findIndex(item=>item.id === currentQustion.id)
      const findTotalIndex = this.data.question.findIndex(item=>item.id === currentQustion.id)


      this.setData({
        questionTypeName:questionConst,
        questionTotal:this.data.question.length,
        questionNo:findTotalIndex,
        questionTypeNo:findIndex,
        questionTypeSum:tempQuestionList.length
     })
  },

 

  preClick:function(){
       if(this.data.questionIndex>0){
         this.setData({
            questionIndex:(this.data.questionIndex-1)
         })
       }
       this.computedQuestionName();
  },
  redirctClick:function(){

    wx.redirectTo({  
      url: '/pages/my-exam-list/my-exam-list?type='+this.data.projectType
     });
  },
  checkAnswer:function(){
    let isCheck=false;
    let question= this.data.question[this.data.questionIndex];
    for(let i=0;i<question.answer.length;i++){
      if(question.answer[i].check){
        isCheck=true;
        break;
      }
    }
    if(!isCheck){
      wx.showToast({  
        title: '请选择一个正确答案',  
        icon: 'none',  
        duration: 3000  
      });  
      return false;
    }
    return true;
  },
  checkAllAnswer:function(){
    for(let i=0;i<this.data.question.length;i++){
      let isCheck=false;  
      for(let j=0;j<this.data.question[i].answer.length;j++){
            if(this.data.question[i].answer[j].check){
              isCheck=true;
              break;
            }
        }
        if(!isCheck){
          wx.showToast({  
            title: '每道题必须选择一道正确答案',  
            icon: 'none',  
            duration: 3000  
          });  
          return false;
        }
    }
    return true;
  },
  nextClick:function(){      
     if(!this.checkAnswer()){
        return;
     }
    if(this.data.questionIndex<(this.data.question.length-1)){
      this.setData({
         questionIndex:(this.data.questionIndex+1)
      })
    }
    this.computedQuestionName();
  },

  //二次确认
  tapDialogConfirm(e){
    //点击确定
    
    if(e.detail.index==1){
      this.summitClick()
    }
    this.setData({
      confirmAgainShow:false
    })
  },

  doShowDialogConfirm(){

    console.log("111",this.checkAllAnswer())

    if(!this.checkAllAnswer()){
      return;
    }
    this.setData({
      confirmAgainShow:true
    })
  },

  async summitClick(){

    let data=[];
    var item={};
     item.sapId=wx.getStorageSync('userinfo').siocSapNo;
     item.staffId=wx.getStorageSync('userinfo').staffId;
     item.trainId=this.data.trainId;
     item.courseId=this.data.courseId;

     //二次确认


    for(var  index=0;index<this.data.question.length;index++){
      var question={};
      question.questionId=this.data.question[index].id;  
      var answer=[];
      for(var j=0;j<this.data.question[index].answer.length;j++){
        if(this.data.question[index].answer[j].check){
            answer.push({key:this.data.question[index].answer[j].key})
          }
      }
      question.answers=answer;
      data.push(question);
    }
    item.questions=data;
     var that=this;

    wx.request({
      url: that.data.baseUrl+'/v1/openApi/staff/examine',
      method: 'POST',
      data:item,
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        if(res.data.code!=200){
          wx.showToast({  
            title: res.data.msg,  
            icon: 'none',  
            duration: 3000  
          });  
          return;
        }
        wx.redirectTo({  
          url: '/pages/my-error/my-error?examId='+res.data.data.id 
         });
      },fail(res){
          wx.showToast({  
            title: '考题提交失败，请联系管理人员！',  
            icon: 'none',  
            duration: 3000  
          });  
      }
    });
  },
  checkChange:function(e){
    console.log('checkChange发生change事件，携带value值为：', e.detail.value);  
    // 更新数据以反映新的选中项  
    let questionList=this.data.question;
    let questionIndex=this.data.questionIndex;
    const question = questionList[questionIndex];  
    for (let i = 0; i< question.answer.length; ++i) {  
      question.answer[i].check= e.detail.value.includes(question.answer[i].key);  
    }  
    this.setData({  
      question: questionList  
    });  
  },
  radioChange: function(e) {  
    console.log('radio发生change事件，携带value值为：', e.detail.value);  
    // 更新数据以反映新的选中项  
    let questionList=this.data.question;
    let questionIndex=this.data.questionIndex;
    const question = questionList[questionIndex];  
    for (let i = 0; i< question.answer.length; ++i) {  
      question.answer[i].check = question.answer[i].key === e.detail.value;  
    }  
    this.setData({  
      question: questionList  
    });  
  }  
});